package com.cpi.cornpp.costelement.services;

import java.util.ArrayList;
import java.util.Collection;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.costelement.converter.CostElementConverter;
import com.cpi.cornpp.costelement.dao.CostElementDAO;
import com.cpi.cornpp.costelement.dto.CostElementDTO;
import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.costelement.view.CostElementView;


public class CostElementServices {
	/**
	 * 
	 * @param costElementView
	 * @return
	 * @throws CostElementException
	 */
	public Collection getCostElementList(int orderField, boolean ascendingOrder) throws CostElementException {
		CostElementDAO costElementDAO = null;
		CostElementConverter costElementConverter = null;
		Collection collectionCostElementDTO = null;
		Collection collectionCostElementView = null;
		try {
			costElementDAO = new CostElementDAO();
			costElementConverter = new CostElementConverter();
			collectionCostElementDTO = new ArrayList(0);
			collectionCostElementView = new ArrayList(0);
			// Obtengo el listado
			collectionCostElementDTO = costElementDAO.getList(orderField, ascendingOrder);
			// Mando convertir el listado en la vista de retorno
			collectionCostElementView = costElementConverter.convertDTOs(collectionCostElementDTO);
		} catch (CostElementException costElementException) {			
			costElementException.printStackTrace(System.out);
			costElementException= new CostElementException(costElementException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw costElementException;
		}
		catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw costElementException;
		}
		return collectionCostElementView;
	}

	/**
	 * 
	 * @return
	 * @throws CostElementException
	 */
	public Collection getCostElementList() throws CostElementException {
		CostElementDAO costElementDAO = null;
		CostElementConverter costElementConverter = null;
		Collection collectionCostElementDTO = null;
		Collection collectionCostElementView = null;
		try {
			costElementDAO = new CostElementDAO();
			costElementConverter = new CostElementConverter();
			collectionCostElementDTO = new ArrayList(0);
			collectionCostElementView = new ArrayList(0);
			// Obtengo el listado
			collectionCostElementDTO = costElementDAO.getList();
			// Mando convertir el listado en la vista de retorno
			collectionCostElementView = costElementConverter.convertDTOs(collectionCostElementDTO);
		} catch (CostElementException costElementException) {			
			costElementException.printStackTrace(System.out);
			costElementException= new CostElementException(costElementException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw costElementException;
		}
		catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw costElementException;
		}
		return collectionCostElementView;
	}
	
	
	public Collection getCostElementList(int purchaseOrderVersionId) throws CostElementException {
		CostElementDAO costElementDAO = null;
		CostElementConverter costElementConverter = null;
		Collection collectionCostElementDTO = null;
		Collection collectionCostElementView = null;
		try {
			costElementDAO = new CostElementDAO();
			costElementConverter = new CostElementConverter();
			collectionCostElementDTO = new ArrayList(0);
			collectionCostElementDTO = costElementDAO.getList(purchaseOrderVersionId);
			collectionCostElementView = costElementConverter.convertDTOs(collectionCostElementDTO);
		} catch (CostElementException costElementException) {			
			costElementException.printStackTrace(System.out);
			costElementException= new CostElementException(costElementException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw costElementException;
		}
		catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw costElementException;
		}
		return collectionCostElementView;
	}

	
	/**
	 * Insert Cost Element
	 * @param costElementView
	 * @return
	 * @throws CostElementException
	 */
	public Integer insertCostElement(CostElementView costElementView) throws CostElementException{
		int costElementId = 0;
		Integer costElementIdentifier = null;
		CostElementDAO costElementDAO = null;
		CostElementConverter costElementConverter = null;
		CostElementDTO costeElementDTO = null;
		try{
			costeElementDTO =  new CostElementDTO();
			costElementDAO = new CostElementDAO();
			costElementConverter = new CostElementConverter(); 
			costeElementDTO = costElementConverter.convertViewToDTO(costElementView);
			costElementId = costElementDAO.insertCostElement(costeElementDTO);
			
			if (costElementId != -1){
				costElementIdentifier = new Integer(costElementId);
			}
			
		}catch (CostElementException costElementException){
			throw costElementException;
		}catch (Exception exception){
			CostElementException costElementException = null;
			exception.printStackTrace();
			costElementException= new CostElementException (costElementException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw costElementException;
		}
		
		return costElementIdentifier;
	}	
	/**
	 * Update a cost element
	 * @param costElementView
	 * @return TRUE > Updated correctly , FALSE > update fails
	 * @throws CostElementException
	 */
	public void updateCostElement(CostElementView costElementView) throws CostElementException{
		CostElementDAO costElementDAO = null;
		CostElementConverter costElementConverter = null;
		CostElementDTO costElementDTO = null;
		try{
			costElementDTO =  new CostElementDTO();
			costElementDAO = new CostElementDAO();
			costElementConverter = new CostElementConverter();
			costElementDTO = costElementConverter.convertViewToDTO(costElementView);
			costElementDAO.editCostElement(costElementDTO);
		}
		catch (CostElementException costElementException){
			throw costElementException;
		}
		catch (Exception exception){
			CostElementException costElementException = null;
			exception.printStackTrace();
			costElementException= new CostElementException (costElementException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw costElementException;
		}
	}		
	
	/**
	 * Delete a cost element
	 * @param costElementView
	 * @return TRUE > Record Deleted correctly , FALSE > delete fails
	 * @throws CostElementException
	 */
	public void deleteCostElement(int costElementId) throws CostElementException{
		CostElementDAO costElementDAO = null;
		try{
			costElementDAO = new CostElementDAO();
			costElementDAO.deleteCostElement(costElementId);
		}
		catch (CostElementException costElementException){
			throw costElementException;
		}
		catch (Exception exception){
			CostElementException costElementException = null;
			exception.printStackTrace();
			costElementException= new CostElementException (costElementException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw costElementException;
		}
	}

	
	/**
	 * Obtiene un CostElement por su code
	 * @param code
	 * @return
	 * @throws CostElementException
	 */
	public CostElementView getCostElementById(int id) throws CostElementException {
		CostElementDAO costElementDAO = null;
		CostElementConverter costElementConverter = null;
		CostElementDTO costElementDTO = null;
		CostElementView costElementView = null;
		try {
			costElementDAO = new CostElementDAO();
			costElementConverter = new CostElementConverter();
			costElementDTO = new CostElementDTO();
			costElementView = new CostElementView();
			// Obtengo el listado			
			costElementDTO = costElementDAO.getById(id);
			// Mando convertir el listado en la vista de retorno
			costElementView = costElementConverter.convert(costElementDTO);
						
		} catch (CostElementException costElementException) {			
			costElementException.printStackTrace(System.out);
			costElementException= new CostElementException(costElementException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw costElementException;
		} catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw costElementException;
		}
		return costElementView;
	}	
}
